package cn.tedu.front.job.controller;

import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.po.UserStatePO;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.common.web.JsonResult;
import cn.tedu.front.job.pojo.dto.PositionAddNewParam;
import cn.tedu.front.job.pojo.dto.PositionUpdateInfoParam;
import cn.tedu.front.job.pojo.vo.PositionDetailVO;
import cn.tedu.front.job.service.IPositionService;

import cn.tedu.zhaopin.protocol.position.vo.PositionSimpleVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Api(tags = "1.企业职位管理")
@Slf4j
@RestController
@RequestMapping("/v1/job/recruit")
public class PositionController {
    @Autowired
    private IPositionService positionService;

    public PositionController() {
        log.debug("创建控制器类对象：PositionController");
    }

    @PreAuthorize("hasAuthority('/zhaopin')")
    @PostMapping("/{id}/add-new")
//    @PreAuthorize("hasAuthority('/enterprise/jobs/simple')")
    @ApiOperation("发布职位")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@PathVariable Long id, PositionAddNewParam positionAddNewParam) {
        log.debug("开始处理【发布职位】的请求，参数：{}", positionAddNewParam);
        positionService.addNew(id,positionAddNewParam);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/zhaopin')")
    @PostMapping("/{id:[0-9]+}/delete")
//    @PreAuthorize("hasAuthority('/mall/goods/delete')")
    @ApiOperation("根据ID删除职位")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "long"))
    public JsonResult deleteById(/*@AuthenticationPrincipal*/ @ApiIgnore CurrentPrincipal currentPrincipal,
                             @PathVariable @Range(min = 1, message = "请提交有效的职位ID值！") Long id) {
        log.debug("开始处理【根据ID删除职位】的请求，参数：{}", id);
        positionService.deleteById(id);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/zhaopin')")
    @PostMapping("/{id:[0-9]+}/update-info")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperation("修改职位基本信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(/*@AuthenticationPrincipal @ApiIgnore*/ CurrentPrincipal currentPrincipal,
                                                                             @PathVariable @Range(min = 1, message = "请提交有效的职位ID值！") Long id,
                                                                             @Valid PositionUpdateInfoParam positionUpdateInfoParam) {
        log.debug("开始处理【修改职位基本信息】的请求，参数：{}", id);
        positionService.updateInfoById(currentPrincipal, id, positionUpdateInfoParam);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/zhaopin')")
    @GetMapping("/detail/{id:[0-9]+}")
    //@PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperation("根据ID查询职位信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的职位ID值！") Long id) {
        log.debug("开始处理【根据ID查询职位】的请求，参数：{}", id);
        PositionDetailVO queryResult = positionService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @PreAuthorize("hasAuthority('/zhaopin')")
    @GetMapping("/list/{id}/page/{pageNumber}")
    //@PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperation("查询职位列表")
    @ApiOperationSupport(order = 420)

    public JsonResult manageList(@PathVariable Long id,@PathVariable Integer pageNumber) {
        log.debug("开始处理【查询订单列表】的请求，页码：{}", pageNumber);
        Integer pageNum = pageNumber == null ? 1 : pageNumber;
        Integer pageSize = 10;
        PageData<PositionSimpleVO> pageData = positionService.list(id,pageNum,pageSize);
        return JsonResult.ok(pageData);
    }

    //TODO  添加字段enable 关闭或者开启职位





    //TODO 添加搜索请求职位  使用es搜索user对应的职位  ?




//    @PostMapping("/{id:[0-9]+}/check-state/pass")
//    @PreAuthorize("hasAuthority('/mall/goods/check')")
//    @ApiOperation("审核通过商品")
//    @ApiOperationSupport(order = 310)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long"),
//            @ApiImplicitParam(name = "remarks", value = "备注信息", required = true, paramType = "query", dataType = "string")
//    })
//    public JsonResult passCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                                @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id,
//                                @NotBlank(message = "请提交审核备注信息") String remarks) {
//        log.debug("开始处理【审核通过商品】的请求，参数：{}", id);
//        goodsService.passCheck(currentPrincipal, id, remarks);
//        return JsonResult.ok();
//    }
//
//    @PostMapping("/{id:[0-9]+}/check-state/reject")
//    @PreAuthorize("hasAuthority('/mall/goods/check')")
//    @ApiOperation("拒绝审核商品")
//    @ApiOperationSupport(order = 311)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long"),
//            @ApiImplicitParam(name = "remarks", value = "备注信息", required = true, paramType = "query", dataType = "string")
//    })
//    public JsonResult rejectCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                                  @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id,
//                                  @NotBlank(message = "请提交审核备注信息") String remarks) {
//        log.debug("开始处理【拒绝审核商品】的请求，参数：{}", id);
//        goodsService.rejectCheck(currentPrincipal, id, remarks);
//        return JsonResult.ok();
//    }
//
//    @PostMapping("/{id:[0-9]+}/put-on")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
//    @ApiOperation("上架商品")
//    @ApiOperationSupport(order = 320)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
//    })
//    public JsonResult putOn(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                            @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
//        log.debug("开始处理【上架商品】的请求，参数：{}", id);
//        goodsService.putOn(currentPrincipal, id);
//        return JsonResult.ok();
//    }
//
//    @PostMapping("/{id:[0-9]+}/pull-off")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
//    @ApiOperation("下架商品")
//    @ApiOperationSupport(order = 321)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
//    })
//    public JsonResult pullOff(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                              @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
//        log.debug("开始处理【下架商品】的请求，参数：{}", id);
//        goodsService.pullOff(currentPrincipal, id);
//        return JsonResult.ok();
//    }
//
//    @PostMapping("/{id:[0-9]+}/set-recommend")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
//    @ApiOperation("推荐商品")
//    @ApiOperationSupport(order = 330)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
//    })
//    public JsonResult setRecommend(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                                   @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
//        log.debug("开始处理【推荐商品】的请求，参数：{}", id);
//        goodsService.setRecommend(currentPrincipal, id);
//        return JsonResult.ok();
//    }
//
//    @PostMapping("/{id:[0-9]+}/cancle-recommend")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
//    @ApiOperation("取消推荐商品")
//    @ApiOperationSupport(order = 331)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
//    })
//    public JsonResult cancelRecommend(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
//                                      @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
//        log.debug("开始处理【取消推荐商品】的请求，参数：{}", id);
//        goodsService.cancelRecommend(currentPrincipal, id);
//        return JsonResult.ok();
//    }
//

//    @GetMapping("/list-by-category")
//    @PreAuthorize("hasAuthority('/mall/goods/simple')")
//    @ApiOperation("根据类别查询商品列表")
//    @ApiOperationSupport(order = 421)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "categoryId", value = "商品类别ID", required = true, paramType = "query", dataType = "long"),
//            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
//    })
//    public JsonResult listByCategory(@Range(message = "请提交有效的商品类别ID值！") Long categoryId,
//                                     @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
//        log.debug("开始处理【根据类别查询商品列表】的请求，父级商品：{}，页码：{}", categoryId, page);
//        Integer pageNum = page == null ? 1 : page;
//        PageData<GoodsListItemVO> pageData = goodsService.listByCategory(categoryId, pageNum);
//        return JsonResult.ok(pageData);
//    }
//
//    @PostMapping("/rebuild-search")
//    @PreAuthorize("hasAuthority('/mall/goods/rebuild-search')")
//    @ApiOperation("重建商品的搜索数据")
//    @ApiOperationSupport(order = 500)
//    public JsonResult rebuildSearch() {
//        log.debug("开始处理【重建商品的搜索数据】的请求");
//        goodsService.rebuildSearch();
//        return JsonResult.ok();
//    }


}
